SYS_RES_FOLDER=/home/laijun/projects/Omni_swarm_ws/deps/sys_res
SYS_RES_BUILD_FOLDER=$SYS_RES_FOLDER/build

ROS_DIST=noetic

OPENCV_FILE="opencv-4.5.4"
OPENCV_CONTRIB_FILE="opencv_contrib-4.5.4"
CUDA_ARCH="7.5"

CERES_FILE="ceres-solver.2.1.0"

NEW_CMAKE_FILE="cmake-3.19.2-Linux-x86_64"

ROS_CV_PACK="vision_opencv-$ROS_DIST"

LIB_SGM_FILE="libSGM.2.8.0"
FAISS_FILE="faiss-1.7.3"

LIB_TORCH_FILE_ZIP="libtorch-cxx11-abi-shared-with-deps-1.12.1+cu113.zip"

# sys res folder manipulate
init_sys_env(){
    if [ -d $SYS_RES_BUILD_FOLDER ];
    then 
        echo "$SYS_RES_BUILD_FOLDER exists, do nothing"
    else 
        echo "$SYS_RES_BUILD_FOLDER not exists"
        mkdir $SYS_RES_BUILD_FOLDER
    fi
}

clear_sys_res_build_env(){
    if [ -d $SYS_RES_BUILD_FOLDER ];
    then
        echo "$SYS_RES_BUILD_FOLDER exists, clearing"
        rm $SYS_RES_BUILD_FOLDER/* -rf
    fi
}

install_dependencies(){
    echo "install dependencies"
    # CMake
    sudo apt-get install -y cmake
    # google-glog + gflags
    sudo apt-get install -y libgoogle-glog-dev libgflags-dev
    # Use ATLAS for BLAS & LAPACK
    sudo apt-get install -y libatlas-base-dev
    # Eigen3
    sudo apt-get install -y libeigen3-dev
    # SuiteSparse (optional)
    sudo apt-get install -y libsuitesparse-dev
    # graphviz (for swarm)
    sudo apt-get install -y graphviz graphviz-doc libgraphviz-dev
    sudo apt-get install -y libdw-dev

    # for opencv build 
    sudo apt-get install -y libv4l-dev
    # for faiss
    sudo apt-get install -y swig
}
install_backward_hpp(){
    echo "install backward.hpp"
    cd $SYS_RES_FOLDER
    sudo cp backward.hpp /usr/include
}

install_ros () {
    echo "install ros $ROS_DIST, with no gazebo inside"
    UBUNTU_DIST=$(lsb_release -sc)
    sudo bash -c "echo \"deb https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ $UBUNTU_DIST main\" > /etc/apt/sources.list.d/ros-latest.list && apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654"

    sudo apt-get update 
    sudo apt-get install -y ros-$ROS_DIST-desktop
    sudo apt-get install -y ros-$ROS_DIST-mavros ros-$ROS_DIST-mavros-extras ros-$ROS_DIST-control-toolbox
    sudo apt install -y ros-$ROS_DIST-rosbridge-suite
}

change_ros_folder_attr(){
    echo "change ros folder's owner and group for the current user"
    USER_NAME=`whoami`
    sudo chown -R $USER_NAME /opt/ros
    sudo chgrp -R $USER_NAME /opt/ros
    ls /opt/ros -al
}

remove_ros_cv(){
    echo "remove ros cv: libopencv* cv_bridge"
    sudo apt remove -y libopencv*  ros-noetic-cv-bridge
}

install_ceres(){
    echo "install ceres"
    echo "This is not official ceres, we modify the cmake settings, see https://blog.csdn.net/MWY123_/article/details/124322024"
    cd $SYS_RES_FOLDER
    tar xvaf "$CERES_FILE.tar.gz"  -C  $SYS_RES_BUILD_FOLDER
    cd $SYS_RES_BUILD_FOLDER/ceres-solver
    mkdir build 
    cd build
    cmake ../
    make -j3
    make test
    sudo make install
}

install_opencv_with_cuda () {
    echo "install opencv with cuda, make sure your cuda toolkit is correctly installed"
    cd $SYS_RES_FOLDER
    tar xvaf "$OPENCV_FILE.tar.gz"   -C  $SYS_RES_BUILD_FOLDER
    tar xvaf "$OPENCV_CONTRIB_FILE.tar.gz"  -C  $SYS_RES_BUILD_FOLDER
    cd $SYS_RES_BUILD_FOLDER/$OPENCV_FILE
    mkdir build
    cd build 
    cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_DISABLE_FIND_PACKAGE_Ceres=ON \
      -D CMAKE_C_COMPILER=gcc \
      -D CMAKE_CXX_COMPILER=g++ \
      -D CMAKE_INSTALL_PREFIX=/usr \
      -D OPENCV_ENABLE_NONFREE=ON \
      -D WITH_CUDA=ON \
      -D CUDA_ARCH_PTX="" \
      -D CUDA_ARCH_BIN=$CUDA_ARCH \
      -D CUDA_FAST_MATH=1 \
      -D WITH_CUBLAS=1 \
      -D WITH_LAPACK=0 \
      -D WITH_NVCUVID=0 \
      -D WITH_TBB=OFF \
      -D WITH_IPP=OFF \
      -D WITH_OPENGL=ON \
      -D WITH_OPENCL=OFF \
      -D WITH_QT=ON \
      -D WITH_GTK=OFF \
      -D Ceres_DIR=/usr/local/lib/cmake/Ceres \
      -D FORCE_VTK=ON \
      -D WITH_EIGEN=ON \
      -D EIGEN_INCLUDE_DIR=/usr/include/eigen3 \
      -D WITH_XINE=ON \
      -D WITH_GDAL=ON \
      -D WITH_1394=OFF \
      -D WITH_FFMPEG=ON \
      -D WITH_GSTREAMER=ON \
      -D HAVE_VIDEOIO=1 \
      -D WITH_V4L=OFF \
      -D WITH_LIBV4L=ON \
      -D BUILD_PROTOBUF=ON \
      -D BUILD_JAVA=OFF \
      -D BUILD_JASPER=ON \
      -D BUILD_opencv_python2=OFF \
      -D BUILD_opencv_python3=ON \
      -D OPENCV_EXTRA_MODULES_PATH=../../$OPENCV_CONTRIB_FILE/modules \
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D INSTALL_C_EXAMPLES=ON \
      -D BUILD_PERF_TESTS=OFF \
      -D BUILD_EXAMPLES=OFF ..
    
    make -j4
    sudo make install 
}

install_new_cmake(){
    echo "install new cmake file to /opt, you need to type y in the process"
    sudo cp $SYS_RES_FOLDER/$NEW_CMAKE_FILE.sh /opt
    sudo chmod +x /opt/$NEW_CMAKE_FILE.sh
    cd /opt
    sudo bash $NEW_CMAKE_FILE.sh --skip-license --prefix=/opt/
    sudo rm $NEW_CMAKE_FILE.sh
    sudo ln -s /opt/$NEW_CMAKE_FILE/bin/* /usr/local/bin
    echo "New cmake version:"
    cmake --version
}

rebuild_ros_cv(){
    echo "rebuild ros cv"
    cd $SYS_RES_FOLDER
    mkdir -p $SYS_RES_BUILD_FOLDER/vision_opencv_ws/src
    tar xvaf "$ROS_CV_PACK.tar.gz"   -C  $SYS_RES_BUILD_FOLDER/vision_opencv_ws/src
    cd $SYS_RES_BUILD_FOLDER/vision_opencv_ws
    
    if [ `echo $0` = bash ];
    then 
        SHELL_USE=bash
    else 
        # other shell not supported by now
        SHELL_USE=zsh
    fi
    . /opt/ros/$ROS_DIST/setup.$SHELL_USE
    catkin_make -DCMAKE_INSTALL_PREFIX=/opt/ros/$ROS_DIST  install --force-cmake
    rospack find cv_bridge
}

install_libSGM(){
    echo "install lib SGM, warning, the cmakefile is changed according to https://github.com/xuhao1/VINS-Fisheye/issues/11"
    cd $SYS_RES_FOLDER
    tar xvaf "$LIB_SGM_FILE.tar.gz"  -C  $SYS_RES_BUILD_FOLDER
    cd $SYS_RES_BUILD_FOLDER/libSGM
    mkdir build 
    cd build
    cmake ../
    make -j3
    sudo make install
}

install_faiss(){
    echo "install faiss"
    cd $SYS_RES_FOLDER
    tar xvaf "$FAISS_FILE.tar.gz"  -C  $SYS_RES_BUILD_FOLDER
    cd $SYS_RES_BUILD_FOLDER/$FAISS_FILE
    mkdir build 
    echo "warning CMAKE_CUDA_ARCHITECTURES is not set with paras"
    cmake -DCUDAToolkit_ROOT=/usr/local/cuda -DCMAKE_CUDA_ARCHITECTURES="75;72" -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_TESTING=ON -DBUILD_TESTING=ON  -B build .
    make -C build -j4 faiss
    sudo make -C build install
    make -C build test
}

install_lib_torch(){
    echo "install lib torch to /opt"
    cd $SYS_RES_FOLDER
    sudo unzip $LIB_TORCH_FILE_ZIP -d /opt
    echo "change libtorch folder's owner and group for the current user"
    USER_NAME=`whoami`
    sudo chown -R $USER_NAME /opt/libtorch
    sudo chgrp -R $USER_NAME /opt/libtorch
    ls /opt/libtorch -al
}

main(){
    # clear_sys_res_build_env

    # install_dependencies
    # install_backward_hpp
    # install_ceres
    # install_ros
    # change_ros_folder_attr
    # remove_ros_cv
    # init_sys_env
    # install_opencv_with_cuda
    # install_new_cmake
    # rebuild_ros_cv
    # install_libSGM
    # install_faiss
    # install_lib_torch
    # pwd
    :
}

PWD_PATH=`pwd`
# echo $PWD_PATH
echo "please run it with source, like source ./auto_install_env.sh"
main && \
echo "all done, enjoy!"
# echo "cd to $PWD_PATH"

cd $PWD_PATH